<?php 
require_once ("../Login/config.php");
include_once '../Login/databaseconnection.php';
	  if ($_FILES["file"]["error"] > 0)
  {
 $_SESSION['error']="Error: " . $_FILES["file"]["error"] . "<br />";
  }

if (file_exists("Upload/" . $_FILES["file"]["name"]))
      {
     $_SESSION['errorex']=$_FILES["file"]["name"] . " already exists.". "<br />";
      }
    else
      {
	    move_uploaded_file($_FILES["file"]["tmp_name"],
      "Upload/" . $_FILES["file"]["name"]);
	  $header = null; 
    $data = array(); 
    $csvData=str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
	$file_name='Upload/'.$_FILES["file"]["name"];
    $delimiter = ",";
    foreach($csvData as $csvLine){ 

if(is_null($header))
{ 
$header = explode($delimiter, $csvLine);
} 
	  $m = count($header);
		 $sqlcols=mysql_query("SHOW COLUMNS FROM teacher");
		 $nr=mysql_num_rows($sqlcols);
		 }
		 if($m!=$nr-1)
		{
		$_SESSION['errorinuploading']="Please check the inputs in the CSV file!<br/>";
		}
		if($m==$nr-1)
		{
    $header = null; 
    $data = array(); 
    $csvData=str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
    $delimiter = ",";
    foreach($csvData as $csvLine){ 

if(is_null($header))
{ 
$header = explode($delimiter, $csvLine);
} 
        else{
	date_default_timezone_set("Asia/Colombo");
    $t_d=date("Y/m/d");
		$m = count($header);
		 $sqlcols=mysql_query("SHOW COLUMNS FROM teacher");
		 $nr=mysql_num_rows($sqlcols);
		
		if($m==$nr-1)
		{
		$items = explode($delimiter, $csvLine); 
		if($items[0]=="")
			{
			$_SESSION['errordata']="Not enough inputs Please recheck the inputs and try again!<br/>";
			}
			else
			{
			$checking=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$items[0]'");
			if($checking)
			{
			if(mysql_num_rows($checking)!=0)
			{
			$_SESSION['errorexi']=$_SESSION['errorexi'].'<img src="../images/alert.jpg" width="12" height="12"/>'.$items[0]."Already exists!<br/>";
			}
			else
			{
			  $sql="INSERT INTO teacher(T_NIC_Number,School_Id,First_Name,Last_Name,Date_Of_Birth,Gender,Nature_Of_Appointment,Service,Academic_Qualifications,Professional_Qualifications,Specialised_In,Achievements_Awards,Address,Mobile_Tp_Number,Home_Tp_Number,Office_Tp_Number,Role,Appointment_Date,Appointed_Subject,Last_Updated_Date) VALUES(";
            
            for($n = 0, $m = count($header); $n < $m; $n++)
			{ 
               
					$sql.= "'".$items[$n]."',";
            } 
			 if($n == $nr-1) 
				 {
					$sql.= "'".$t_d."'";
					}
			$sql.= ")";
             $inse=mysql_query($sql);
			if(mysql_affected_rows==0)
			 {			 
             unlink("$file_name"); 
			 }
			else if($inse&&isset($_SESSION['errorexi']))
			{
			$_SESSION['insertsucc']="Others are successfully inserted!";
			}
			else if($inse&&!isset($_SESSION['errorexi']))
			{
			if($m>1)
			{
			$_SESSION['insertsucc']="All are successfully inserted!";
			}
			if($m==1)
			{
			$_SESSION['insertsucc']="Teacher's details are successfully inserted!";
			}
			}
			}
			}
			}
          }
        } 
    } 
	}
	}
 mysql_close($conn);
?> 